home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_Tix.idb / usr / freeware / lib / tix4.1 / SWindow.tcl.z / SWindow.tcl
Encoding:
Text File  |  1999-01-26  |  6.7 KB  |  273 lines

  1. # SWindow.tcl --
  2. #
  3. #    This file implements Scrolled Window widgets
  4. #
  5. # Copyright (c) 1996, Expert Interface Technologies
  6. #
  7. # See the file "license.terms" for information on usage and redistribution
  8. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  9. #
  10.  
  11. #
  12. #
  13. # Example:
  14. #    
  15. #    tixScrolledWindow .w
  16. #    set window [.w subwidget window]
  17. #        # Now you can put a whole widget hierachy inside $window.
  18. #        #
  19. #    button $window.b
  20. #    pack $window.b
  21. #
  22. # Author's note
  23. #
  24. # Note, the current implementation does not allow the child window
  25. # to be outside of the parent window when the parent's size is larger
  26. # than the child's size. This is fine for normal operations. However,
  27. # it is not suitable for an MDI master window. Therefore, you will notice
  28. # that the MDI master window is not a subclass of ScrolledWidget at all.
  29. #
  30. #
  31.  
  32. tixWidgetClass tixScrolledWindow {
  33.     -classname TixScrolledWindow
  34.     -superclass tixScrolledWidget
  35.     -method {
  36.     }
  37.     -flag {
  38.     -expandmode -shrink -xscrollincrement -yscrollincrement
  39.     }
  40.     -static {
  41.     }
  42.     -configspec {
  43.     {-expandmode expandMode ExpandMode expand}
  44.     {-shrink shrink Shrink ""}
  45.     {-xscrollincrement xScrollIncrement ScrollIncrement ""}
  46.     {-yscrollincrement yScrollIncrement ScrollIncrement ""}
  47.  
  48.     {-scrollbarspace scrollbarSpace ScrollbarSpace {both}}
  49.     }
  50.     -default {
  51.     {.scrollbar            auto}
  52.     {*window.borderWidth        1}
  53.     {*f1.borderWidth        1}
  54.     {*Scrollbar.borderWidth        1}
  55.     {*Scrollbar.background        #d9d9d9}
  56.     {*Scrollbar.relief        sunken}
  57.     {*Scrollbar.troughColor        #c3c3c3}
  58.     {*Scrollbar.takeFocus        0}
  59.     {*Scrollbar.width        15}
  60.     }
  61. }
  62.  
  63. proc tixScrolledWindow:InitWidgetRec {w} {
  64.     upvar #0 $w data
  65.  
  66.     tixChainMethod $w InitWidgetRec
  67.  
  68.     set data(dx) 0
  69.     set data(dy) 0
  70. }
  71.  
  72. proc tixScrolledWindow:ConstructWidget {w} {
  73.     upvar #0 $w data
  74.  
  75.     tixChainMethod $w ConstructWidget
  76.  
  77.     set data(pw:f1) \
  78.     [frame $w.f1 -relief sunken]
  79.     set data(pw:f2) \
  80.     [frame $w.f2 -bd 0]
  81.     set data(w:window) \
  82.     [frame $w.f2.window -bd 0]
  83.     pack $data(pw:f2) -in $data(pw:f1) -expand yes -fill both
  84.  
  85.     set data(w:hsb) \
  86.     [scrollbar $w.hsb -orient horizontal -takefocus 0]
  87.     set data(w:vsb) \
  88.     [scrollbar $w.vsb -orient vertical -takefocus 0]
  89. #   set data(w:pann) \
  90. #    [frame $w.pann -bd 2 -relief groove]
  91.     
  92.     $data(pw:f1) config -highlightthickness \
  93.     [$data(w:hsb) cget -highlightthickness]
  94.  
  95.     set data(pw:client) $data(pw:f1)
  96. }
  97.  
  98. proc tixScrolledWindow:SetBindings {w} {
  99.     upvar #0 $w data
  100.  
  101.     tixChainMethod $w SetBindings
  102.  
  103.     $data(w:hsb) config -command "tixScrolledWindow:ScrollBarCB $w x"
  104.     $data(w:vsb) config -command "tixScrolledWindow:ScrollBarCB $w y"
  105.  
  106.     tixManageGeometry $data(w:window) "tixScrolledWindow:WindowGeomProc $w"
  107. }
  108.  
  109. # This guy just keeps asking for a same size as the w:window 
  110. #
  111. proc tixScrolledWindow:WindowGeomProc {w args} {
  112.     upvar #0 $w data
  113.  
  114.     set rw [winfo reqwidth  $data(w:window)]
  115.     set rh [winfo reqheight $data(w:window)]
  116.  
  117.     if {$rw != [winfo reqwidth  $data(pw:f2)] ||
  118.     $rh != [winfo reqheight $data(pw:f2)]} {
  119.     tixGeometryRequest $data(pw:f2) $rw $rh
  120.     }
  121. }
  122.  
  123. proc tixScrolledWindow:Scroll {w axis total window first args} {
  124.     upvar #0 $w data
  125.  
  126.     case [lindex $args 0] {
  127.     "scroll" {
  128.         set amt  [lindex $args 1]
  129.         set unit [lindex $args 2]
  130.  
  131.         case $unit {
  132.         "units" {
  133.             set incr $axis\scrollincrement
  134.             if {$data(-$incr) != ""} {
  135.             set by $data(-$incr)
  136.             } else {
  137.             set by [expr $window / 16]
  138.             }
  139.             set first [expr $first + $amt * $by]
  140.         }
  141.         "pages" {
  142.             set first [expr $first + $amt * $window]
  143.         }
  144.         }
  145.     }
  146.     "moveto" {
  147.         set to [lindex $args 1]
  148.         set first [expr int($to * $total)]
  149.     }
  150.     }
  151.  
  152.     if {[expr $first + $window] > $total} {
  153.     set first [expr $total - $window]
  154.     }
  155.     if {$first < 0} {
  156.     set first 0
  157.     }
  158.  
  159.     return $first
  160. }
  161.  
  162. proc tixScrolledWindow:ScrollBarCB {w axis args} {
  163.     upvar #0 $w data
  164.  
  165.     set bd \
  166.        [expr [$data(pw:f1) cget -bd] + [$data(pw:f1) cget -highlightthickness]]
  167.     set fw [expr [winfo width  $data(pw:f1)] - 2*$bd]
  168.     set fh [expr [winfo height $data(pw:f1)] - 2*$bd]
  169.     set ww [winfo reqwidth  $data(w:window)]
  170.     set wh [winfo reqheight $data(w:window)]
  171.  
  172.     if {$axis == "x"} {
  173.     set data(dx) \
  174.         [eval tixScrolledWindow:Scroll $w $axis $ww $fw $data(dx) $args]
  175.     } else {
  176.     set data(dy) \
  177.         [eval tixScrolledWindow:Scroll $w $axis $wh $fh $data(dy) $args]
  178.     }
  179.  
  180.     tixWidgetDoWhenIdle tixScrolledWindow:PlaceWindow $w
  181. }
  182.  
  183. proc tixScrolledWindow:PlaceWindow {w} {
  184.     upvar #0 $w data
  185.  
  186.     set bd \
  187.        [expr [$data(pw:f1) cget -bd] + [$data(pw:f1) cget -highlightthickness]]
  188.     set fw [expr [winfo width  $data(pw:f1)] - 2*$bd]
  189.     set fh [expr [winfo height $data(pw:f1)] - 2*$bd]
  190.     set ww [winfo reqwidth  $data(w:window)]
  191.     set wh [winfo reqheight $data(w:window)]
  192.  
  193.     tixMapWindow $data(w:window)
  194.  
  195.     if {$data(-expandmode) == "expand"} {
  196.     if {$ww < $fw} {
  197.         set ww $fw
  198.     }
  199.     if {$wh < $fh} {
  200.         set wh $fh
  201.     }
  202.     }
  203.     if {$data(-shrink) == "x"} {
  204.     if {$fw < $ww} {
  205.         set ww $fw
  206.     }
  207.     }
  208.  
  209.     tixMoveResizeWindow $data(w:window) -$data(dx) -$data(dy) $ww $wh
  210.  
  211.     set first [expr $data(dx).0 / $ww.0]
  212.     set last  [expr $first + ($fw.0 / $ww.0)]
  213.     $data(w:hsb) set $first $last
  214.  
  215.     set first [expr $data(dy).0 / $wh.0]
  216.     set last  [expr $first + ($fh.0 / $wh.0)]
  217.     $data(w:vsb) set $first $last
  218. }
  219.  
  220. #----------------------------------------------------------------------
  221. # virtual functions to query the client window's scroll requirement
  222. #
  223. # When this function is called, the scrolled window is going to be
  224. # mapped, if it is still unmapped. Also, it is going to change its
  225. # size. Therefore, it is a good time to check whether the w:window needs
  226. # to be re-positioned due to the new parent window size.
  227. #----------------------------------------------------------------------
  228. proc tixScrolledWindow:GeometryInfo {w mW mH} {
  229.     upvar #0 $w data
  230.  
  231.     set bd \
  232.        [expr [$data(pw:f1) cget -bd] + [$data(pw:f1) cget -highlightthickness]]
  233.     set fw [expr $mW -2*$bd]
  234.     set fh [expr $mH -2*$bd]
  235.     set ww [winfo reqwidth  $data(w:window)]
  236.     set wh [winfo reqheight $data(w:window)]
  237.  
  238.     # Calculate the X info
  239.     #
  240.     if {$fw >= $ww} {
  241.     if {$data(dx) > 0} {
  242.         set data(dx) 0
  243.     }
  244.     set xinfo [list 0.0 1.0]
  245.     } else {
  246.     set maxdx [expr $ww - $fw]
  247.     if {$data(dx) > $maxdx} {
  248.         set data(dx) $maxdx
  249.     }
  250.     set first [expr $data(dx).0 / $ww.0]
  251.     set last  [expr $first + ($fw.0 / $ww.0)]
  252.     set xinfo [list $first $last]
  253.     }
  254.     # Calculate the Y info
  255.     #
  256.     if {$fh >= $wh} {
  257.     if {$data(dy) > 0} {
  258.         set data(dy) 0
  259.     }
  260.     set yinfo [list 0.0 1.0]
  261.     } else {
  262.     set maxdy [expr $wh - $fh]
  263.     if {$data(dy) > $maxdy} {
  264.         set data(dy) $maxdy
  265.     }
  266.     set first [expr $data(dy).0 / $wh.0]
  267.     set last  [expr $first + ($fh.0 / $wh.0)]
  268.     set yinfo [list $first $last]
  269.     }
  270.  
  271.     return [list $xinfo $yinfo]
  272. }
  273.